package com.zhl.media.commonres.view;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhl.media.commonres.R;


public class PublicTitleBar extends FrameLayout {

    private RelativeLayout rlContent;
    private ImageView imgLeft, imgRight;
    private TextView tvTitle, tvRight;
    private LinearLayout llLeft;
    private LinearLayout llRight;
    private Context mContext;
    public PublicTitleBar(@NonNull Context context) {
        this(context, null);
    }

    public PublicTitleBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PublicTitleBar(final @NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.public_title_bar, null);
        imgLeft = (ImageView) view.findViewById(R.id.imgLeft);
        imgRight = (ImageView) view.findViewById(R.id.head_rightimage);
        tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        tvRight = (TextView) view.findViewById(R.id.tvRight);
        llLeft = (LinearLayout) view.findViewById(R.id.head_leftshow);
        llRight = (LinearLayout) view.findViewById(R.id.head_rightshow);
        rlContent =  (RelativeLayout)view.findViewById(R.id.rl_content);
        llLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ((Activity)context).finish();
            }
        });
        addView(view);
    }

    public void setImgLeft(int Left, OnClickListener listener) {
        imgLeft.setImageResource(Left);
        llLeft.setOnClickListener(listener);
    }

    public void setBgColor(int color){
        rlContent.setBackgroundColor(color);
    }

    public void isWhite(){
        imgLeft.setColorFilter(ContextCompat.getColor(mContext, R.color.public_colorPrimary));
        tvTitle.setTextColor(ContextCompat.getColor(mContext, R.color.public_colorPrimary));
        tvRight.setTextColor(ContextCompat.getColor(mContext, R.color.public_colorPrimary));
    }

    public void setLeftLisener(OnClickListener listener) {
        llLeft.setOnClickListener(listener);
    }
    public void setClickListener(OnClickListener listener) {
        this.setOnClickListener(listener);
    }
    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    public void setRight(String right, OnClickListener listener) {
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText(right);
        llRight.setOnClickListener(listener);
    }

    public void setRight(int right, OnClickListener listener) {
        imgRight.setVisibility(VISIBLE);
        imgRight.setImageResource(right);
        llRight.setOnClickListener(listener);
    }

    public void setRight(String right) {
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText(right);
    }

    public ImageView getImgLeft() {
        return imgLeft;
    }

    public ImageView getImgRight() {
        return imgRight;
    }

    public TextView getTvRight() {
        return tvRight;
    }

    public String getTitle() {
        return tvTitle.getText().toString();
    }
}
